'use strict'

define(['jquery', 'bootstrap', 'shiftform','select2', 'validate', 'validate.en', 'koala-search', 'datetimepicker', 'datetimepickerCN'],
function($ ) {
  return {
    
    init: function() {
      this.initPage();
      this.initSearch();
    },
    
    initPage: function() {
      
        $(document).on("shown.bs.modal" , "#create" ,function(e){
        	
        	var oldEffectDate;
        	
             $(document).on("change" , "select[name='organization'] , select[name='oilType']" ,function(e){
                 
                 if($("#searchOrganization").length > 0){
                	 var orgId = $('select[name="organization"]').val().trim();
                 }
                 else{
                	 var orgId = $('input[name="organization"]').val();
                 }
                 
                 var oilType = $('select[name="oilType"]').val().trim();
                 
                 if((orgId != "" && orgId != undefined && orgId != null) && (oilType != "" && oilType != undefined && oilType != null)) {
                        
                        $.ajax({
                            url:window.__CTX__.root + "/oil-prices?prepare=true&orgId="+orgId + "&oilType="+oilType,
                            type:"GET",
                        }).done(function(data){
                            data = typeof data === 'String' ? JSON.parse(data) : data
                            		
                            if(parseInt(data.code) == 0) {  
                            	
                                var response = data.data;
                                
                                if(response.oilPrice != null) {
                                                                        
                                    $(".currentOilPrice").text(response.oilPrice.price);
                                    $(".effectDate").text(response.oilPrice.effectDate);
                                    
                                    if(response.oilPrice.effectDate == null){
                                    	
                                    	$('.date').datetimepicker({
                                            format: 'yyyy-mm-dd hh:ii:ss',
                                            autoclose: true,
                                            language: 'zh-CN',
                                            todayBtn: 'linked',
                                            startDate: '${today}',
                                            todayHighlight: true,
                                            pickerPosition: 'bottom-right',
                                            minView: 0
                                          });
                                    	
                                    }else{
                                    	oldEffectDate = response.oilPrice.effectDate;
//                                    	oldEffectDate = oldEffectDate.substring(0,10);
                                        
                                        $('.date').datetimepicker({
                                            format: 'yyyy-mm-dd hh:ii:ss',
                                            autoclose: true,
                                            language: 'zh-CN',
                                            todayBtn: 'linked',
                                            startDate: oldEffectDate,
                                            todayHighlight: true,
                                            pickerPosition: 'bottom-right',
                                            minView: 0
                                          })
                                          
                                    	 $('.date').datetimepicker() .on('show', function(){
                                    		$('.date').datetimepicker('setStartDate',oldEffectDate)
                                    	})
                                    }
                                    
                                } else {
                                    $(".currentOilPrice").text('无');
                                    $(".effectDate").text('无');
                                }

                            } else {
                                console.log(data.code);
//                                changeBackdropMessage('查询失败', 'text-danger', options.failure, data.data.url)
                            }
                            
                        }).fail(function() {
                            console.log(data.code);
//                          changeBackdropMessage('查询失败', 'text-danger', data.url)
                        })
                 }
                 else{
                	 $(".currentOilPrice").text('');
                     $(".effectDate").text('');
                 }
             })
             
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
            var $form = $(e.currentTarget).find("form")                          //bind shiftform
            $form.shiftform({
              beforeSubmit : function(el) {
                $form.trigger("validate.ajax.submit");
                return $.bt_validate.result[$.bt_validate.form_id]
              }
            })
            
        });
    },
   
    
    
    
    initSearch: function() {
        
        $(document).on("shown.bs.modal" , "#search" ,function(e){
            
            $(e.currentTarget).find('form[data-validate="true"]').bt_validate()  //bind validate
            
        });
    }
    
  }
})